package com.firmboy.springboot_axis2.config;

import org.apache.axis2.AxisFault;
import org.apache.axis2.context.ConfigurationContext;
import org.apache.axis2.deployment.DeploymentException;
import org.apache.axis2.deployment.WarBasedAxisConfigurator;
import org.apache.axis2.deployment.repository.util.ArchiveReader;
import org.apache.axis2.deployment.repository.util.DeploymentFileData;
import org.apache.axis2.description.AxisService;
import org.apache.axis2.description.AxisServiceGroup;
import org.apache.axis2.engine.AxisConfiguration;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.util.ResourceUtils;

import javax.servlet.ServletConfig;
import javax.xml.stream.XMLStreamException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * @author firmboy
 * @create 2018-06-08 下午7:13
 **/
public class WarBasedAxisConfiguratorExport extends WarBasedAxisConfigurator {

    private static final Log log = LogFactory.getLog(WarBasedAxisConfiguratorExport.class);
    private ServletConfig config;

    /**
     * Default constructor for configurator.
     * This determines the axis2.xml file to be used from the init parameters for the AxisServlet in the web.xml.
     * The order of initialization is according the the following precedence:
     * <ul>
     * <li>If the parameter axis2.xml.path is present, the value is webapp relative path to be used as the location to the axis2.xml file.
     * <li>Otherwise, if the parameter axis2.xml.url is present, the URL is used as the location to the axis2.xml file.
     * <li>Otherwise, when both of the above init parameters are not present, file is attempted to be loaded from &lt;repo&gt;/WEB-INF/axis2.xml.
     * <li> When none of the above could be found, the axis2.xml is loaded from the classpath resource, the value of DeploymenConstants.AXIS2_CONFIGURATION_RESOURCE.
     * </ul>
     *
     * @param servletConfig the ServletConfig object from the AxisServlet. This method is called from the init() of the AxisServlet.
     */
    public WarBasedAxisConfiguratorExport(ServletConfig servletConfig) throws DeploymentException {
        super(servletConfig);
        this.config = servletConfig;
    }


    @Override
    public void loadServices() {
        try {
            String repository;

            repository = config.getInitParameter(PARAM_AXIS2_REPOSITORY_PATH);
            if (repository != null) {
                super.loadServices();
                log.debug("loaded services from path: " + repository);
                return;
            }

            repository = config.getInitParameter(PARAM_AXIS2_REPOSITORY_URL);
            if (repository != null) {
                loadServicesFromUrl(new URL(repository));
                log.debug("loaded services from URL: " + repository);
                return;
            }
            loadServicesFromWebInf();
            if (config.getServletContext().getRealPath("") != null ||
                    config.getServletContext().getRealPath("/") != null) {
                super.loadServices();
                log.debug("loaded services from webapp");
                return;
            }

            URL url = config.getServletContext().getResource("/WEB-INF/");
            if (url != null) {
                loadServicesFromUrl(url);
                log.debug("loaded services from /WEB-INF/ folder (URL)");
            }
        } catch (MalformedURLException e) {
            log.info(e.getMessage());
        }
    }


    private void loadServicesFromWebInf() {
        try {
            System.err.println("开始测试");
            String path = ResourceUtils.getURL("classpath:").getPath();
            if(path.startsWith("file")){
                //这是用jar文件或者war文件直接启动的，要从当前jar包的根目录获取配置文件
                int index = path.indexOf("!");
                path = path.substring(0,index);
                index = path.lastIndexOf("/");
                path = path.substring(5,index);
            }
            File file = new File(path + "/services/services.xml");
            InputStream servicexml = new FileInputStream(file);
            if (servicexml != null) {
                System.err.println("测试成功");
                HashMap<String, AxisService> wsdlServices = new HashMap<String, AxisService>();
                ArchiveReader archiveReader = new ArchiveReaderExport();
                String path1 = path +"/services";
                if (path != null) {
                    archiveReader.processFilesInFolder(new File(path), wsdlServices);
                }
                AxisServiceGroup serviceGroup = buildServiceGroup(servicexml,
                        Thread.currentThread().getContextClassLoader(),
                        "annonServiceGroup",
                        configContext,
                        archiveReader,
                        wsdlServices);
                axisConfig.addServiceGroup(serviceGroup);
            }
        } catch (AxisFault axisFault) {
            log.info(axisFault);
        } catch (FileNotFoundException e) {
            log.info(e);
        } catch (XMLStreamException e) {
            log.info(e);
        }
    }

}
